package com.yanqu.road.dao.impl.activity.peakstronghold;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakstronghold.CrossPeakStrongholdDefendUnionData;
import com.yanqu.road.entity.activity.peakstronghold.PeakStrongholdDefendPatrons;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossPeakStrongholdDefendUnionDataDaoImpl extends TempDataObjectDao<CrossPeakStrongholdDefendUnionData> {
    public static String getCrossPeakStrongholdDefendUnionDataSql = "select * from t_c_activity_peak_stronghold_defend_union where activityId=? and unionGroup=?";

    public Map<Integer, Map<String, CrossPeakStrongholdDefendUnionData>> getCrossPeakStrongholdDefendUnionData(int activityId, int unionGroup) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<String, CrossPeakStrongholdDefendUnionData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossPeakStrongholdDefendUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossPeakStrongholdDefendUnionData data = parse(rs);
                Map<String, CrossPeakStrongholdDefendUnionData> groupMap = dataMap.get(data.getRound());
                if (null == groupMap) {
                    groupMap = new ConcurrentHashMap<>();
                    dataMap.put(data.getRound(), groupMap);
                }
                groupMap.put(data.getUnionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossPeakStrongholdDefendUnionDataSql = "update t_c_activity_peak_stronghold_defend_union " +
            "set defendPatrons=?,star=?,hpAdd=?,attackUnionMap=?,defendUnionMap=?,groupRank=?,totalDamage=?,groupScore=?,rank=?,score=?,silverRank=?,silver=? " +
            " where activityId=? and round=? and unionId=? and unionGroup=?";

    @Override
    protected boolean updateData(CrossPeakStrongholdDefendUnionData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDefendPatronsList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStar()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHpAdd()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAttackUnionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDefendUnionMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTotalDamage().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getGroupScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSilverRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getSilver().toString()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnionGroup()));
        if (getDb().executeNoneQuery(updateCrossPeakStrongholdDefendUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossPeakStrongholdDefendUnionDataSql = "insert into t_c_activity_peak_stronghold_defend_union" +
            "(activityId,unionGroup,round,unionId,groupNum,defendPatrons,star,hpAdd,attackUnionMap,defendUnionMap,groupRank,totalDamage,groupScore,rank,score,silverRank,silver) " +
            "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossPeakStrongholdDefendUnionData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnionGroup()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDefendPatronsList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStar()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHpAdd()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAttackUnionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDefendUnionMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTotalDamage().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getGroupScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSilverRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getSilver().toString()));
        if (getDb().executeNoneQuery(insertCrossPeakStrongholdDefendUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossPeakStrongholdDefendUnionData parseData(ResultSet rs) throws SQLException {
        CrossPeakStrongholdDefendUnionData data = new CrossPeakStrongholdDefendUnionData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionGroup(rs.getInt("unionGroup"));
        data.setUnionId(rs.getString("unionId"));
        data.setGroup(rs.getInt("groupNum"));
        data.setRound(rs.getInt("round"));
        data.setDefendPatronsList(JSON.parseObject(rs.getString("defendPatrons"), new TypeReference<List<PeakStrongholdDefendPatrons>>() {
        }));
        data.setStar(rs.getInt("star"));
        data.setHpAdd(rs.getInt("hpAdd"));
        data.setAttackUnionMap(JSON.parseObject(rs.getString("attackUnionMap"), new TypeReference<ConcurrentHashMap<String, ConcurrentHashMap<Long, BigInteger>>>() {
        }));
        data.setDefendUnionMap(JSON.parseObject(rs.getString("defendUnionMap"), new TypeReference<ConcurrentHashMap<String, ConcurrentHashMap<Long, BigInteger>>>() {
        }));
        data.setGroupRank(rs.getInt("groupRank"));
        data.setTotalDamage(new BigInteger(rs.getString("totalDamage")));
        data.setGroupScore(new BigInteger(rs.getString("groupScore")));
        data.setRank(rs.getInt("rank"));
        data.setScore(new BigInteger(rs.getString("score")));
        data.setSilverRank(rs.getInt("silverRank"));
        data.setSilver(new BigInteger(rs.getString("silver")));
        return data;
    }
}
